package cn.e2win.biz.cygs;

import javax.servlet.http.HttpServletRequest;

import cn.e2win.biz.comm.ViewExcel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.base.json.vo.Result;
import cn.e2win.biz.annotation.PageParam;
import cn.e2win.biz.base.BaseController;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.CygsEmp;
import cn.e2win.biz.dbo.CygsOrg;
import cn.e2win.biz.dbo.Project;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.Map;


@Controller
@Scope("request")
@RequestMapping("/cygs")
public class CygsController extends BaseController {

	@Autowired
	private CygsService cygsService;
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseService initService() {
		return cygsService;
	}

	@RequestMapping("toCygsEditor/{id}")
	public String toCygsEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		CygsOrg obj = (CygsOrg)cygsService.getEntity(CygsOrg.class, id);
		modelMap.put(OBJECT, obj);
		/*
		List<SysLabel> labelMainList = commonService.getSysLabelList(id,"cyzjOrg","","labelMain");
		List<SysLabel> labelOfficeList = commonService.getSysLabelList(id,"cyzjOrg","","labelOffice");						
		modelMap.put("labelMainList", labelMainList);
		modelMap.put("labelOfficeList", labelOfficeList);

		List<SysLabel> allLabelMainList = commonService.getSysLabelList(null,"cyzjOrg","","labelMain");
		List<SysLabel> allLabelOfficeList = commonService.getSysLabelList(null,"cyzjOrg","","labelOffice");
		modelMap.put("allLabelMainList", allLabelMainList);
		modelMap.put("allLabelOfficeList", allLabelOfficeList);*/

		return "cygs/cygsEditor";
	}

	@RequestMapping("editCygs")
	@ResponseBody
	public Result editCygs(HttpServletRequest request,@ModelAttribute CygsOrg obj, ModelMap modelMap) throws MyException{
		Result r = new Result();
		cygsService.editCygs(obj);
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		return r;
	}


	@RequestMapping("cygsPager")
	public String cygsPager(HttpServletRequest request, ModelMap modelMap, @PageParam Pager pager){
		if("approve".equals(todo)){
			pager.getScMap().put("approveStatus", "SUBMITED");
		}else if("submit".equals(todo)){
			pager.getScMap().put("approveStatus_IN", "'UNSUBMIT','REJECTED'");
		}
		cygsService.pagerCygs(pager);
		modelMap.put("pager", pager);
		return "cygs/cygsPager";
	}

	@RequestMapping("cygsPagerExcel")
	public ModelAndView customerPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",cygsPager(request,modelMap,pager));
		model.put("list", pager.getResultList());
		return new ModelAndView(new ViewExcel(), model);
	}


	
	
	


	@RequestMapping("toCygsEmpEditor/{id}")
	public String toCygsEmpEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		CygsEmp obj = (CygsEmp)cygsService.getEntity(CygsEmp.class, id);
		modelMap.put(OBJECT, obj);
		/*
		List<SysLabel> labelMainList = commonService.getSysLabelList(id,"cyzjOrg","","labelMain");
		List<SysLabel> labelOfficeList = commonService.getSysLabelList(id,"cyzjOrg","","labelOffice");						
		modelMap.put("labelMainList", labelMainList);
		modelMap.put("labelOfficeList", labelOfficeList);

		List<SysLabel> allLabelMainList = commonService.getSysLabelList(null,"cyzjOrg","","labelMain");
		List<SysLabel> allLabelOfficeList = commonService.getSysLabelList(null,"cyzjOrg","","labelOffice");
		modelMap.put("allLabelMainList", allLabelMainList);
		modelMap.put("allLabelOfficeList", allLabelOfficeList);*/

		return "cygs/cygsEmpEditor";
	}

	@RequestMapping("editCygsEmp")
	@ResponseBody
	public Result editCygsEmp(HttpServletRequest request,@ModelAttribute CygsEmp obj, ModelMap modelMap) throws MyException{
		Result r = new Result();
		cygsService.editCygsEmp(obj);
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		return r;
	}


	@RequestMapping("cygsEmpPager")
	public String cygsEmpPager(HttpServletRequest request, ModelMap modelMap, @PageParam Pager pager){
		if("approve".equals(todo)){
			pager.getScMap().put("approveStatus", "SUBMITED");
		}else if("submit".equals(todo)){
			pager.getScMap().put("approveStatus_IN", "'UNSUBMIT','REJECTED'");
		}
		cygsService.pagerCygsEmp(pager);
		modelMap.put("pager", pager);
		return "cygs/cygsEmpPager";
	}


	@RequestMapping("cygsEmpPagerExcel")
	public ModelAndView cygsEmpPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",cygsEmpPager(request,modelMap,pager));
		model.put("list", pager.getResultList());
		return new ModelAndView(new ViewExcel(), model);
	}
	
	


	@RequestMapping("toProjectEditor/{id}")
	public String toProjectEditor(HttpServletRequest request, @PathVariable(ID) Integer id, ModelMap modelMap){
		Project obj = (Project)cygsService.getEntity(Project.class, id);
		modelMap.put(OBJECT, obj);
		/*
		List<SysLabel> labelMainList = commonService.getSysLabelList(id,"cyzjOrg","","labelMain");
		List<SysLabel> labelOfficeList = commonService.getSysLabelList(id,"cyzjOrg","","labelOffice");						
		modelMap.put("labelMainList", labelMainList);
		modelMap.put("labelOfficeList", labelOfficeList);

		List<SysLabel> allLabelMainList = commonService.getSysLabelList(null,"cyzjOrg","","labelMain");
		List<SysLabel> allLabelOfficeList = commonService.getSysLabelList(null,"cyzjOrg","","labelOffice");
		modelMap.put("allLabelMainList", allLabelMainList);
		modelMap.put("allLabelOfficeList", allLabelOfficeList);*/

		return "cygs/projectEditor";
	}

	@RequestMapping("editProject")
	@ResponseBody
	public Result editProject(HttpServletRequest request,@ModelAttribute Project obj, ModelMap modelMap) throws MyException{
		Result r = new Result();
		cygsService.editProject(obj);
		r.setCallbackType(Result.CLOSE_CURRENT);
		r.setNavTabId("listView_"+modelMap.get(FUNID));
		return r;
	}


	@RequestMapping("projectPager")
	public String projectPager(HttpServletRequest request, ModelMap modelMap, @PageParam Pager pager){
		if("approve".equals(todo)){
			pager.getScMap().put("approveStatus", "SUBMITED");
		}else if("submit".equals(todo)){
			pager.getScMap().put("approveStatus_IN", "'UNSUBMIT','REJECTED'");
		}
		cygsService.pagerProject(pager);
		modelMap.put("pager", pager);
		return "cygs/projectPager";
	}

	@RequestMapping("projectPagerExcel")
	public ModelAndView projectPagerExcel(HttpServletRequest request, ModelMap modelMap) {
		pager.setReturnAll(true);
		Map<String, Object> model = new HashMap<String, Object>();
		model.put("key",projectPager(request,modelMap,pager));
		model.put("list", pager.getResultList());
		return new ModelAndView(new ViewExcel(), model);
	}
}
